package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Programa` (
  `idPrograma` INT NULL AUTO_INCREMENT ,
  `SistemaOperacional_idSistemaOperacional` INT NOT NULL ,
  `Programas_idProgramas` INT NOT NULL ,
  `Licenca_idLicenca` INT NOT NULL ,
  PRIMARY KEY (`idPrograma`) ,
  INDEX `fk_Programa_SistemaOperacional1` (`SistemaOperacional_idSistemaOperacional` ASC) ,
  INDEX `fk_Programa_Programas1` (`Programas_idProgramas` ASC) ,
  INDEX `fk_Programa_Licenca1` (`Licenca_idLicenca` ASC) ,
  CONSTRAINT `fk_Programa_SistemaOperacional1`
    FOREIGN KEY (`SistemaOperacional_idSistemaOperacional` )
    REFERENCES `mydb`.`SistemaOperacional` (`idSistemaOperacional` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Programa_Programas1`
    FOREIGN KEY (`Programas_idProgramas` )
    REFERENCES `mydb`.`Programas` (`idProgramas` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Programa_Licenca1`
    FOREIGN KEY (`Licenca_idLicenca` )
    REFERENCES `mydb`.`Licenca` (`idLicenca` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;*/
public class Programa {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Programa.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idPrograma;
    private SistemaOperacional SistemaOperacional_idSistemaOperacional;
    private Programas Programas_idProgramas;
    private Licenca Licenca_idLicenca;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Programa(int idPrograma) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idPrograma = '" + idPrograma + "'");
            rs.next();
            this.idPrograma = rs.getInt("idPrograma");
            this.SistemaOperacional_idSistemaOperacional = new SistemaOperacional(rs.getInt("SistemaOperacional_idSistemaOperacional"));
            this.Programas_idProgramas = new Programas(rs.getInt("Programas_idProgramas"));
            this.Licenca_idLicenca = new Licenca(rs.getInt("Licenca_idLicenca"));
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Programa:";
            s += "idPrograma=" + idPrograma;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idPrograma() {
        return this.idPrograma;
    }

    public SistemaOperacional SistemaOperacional_idSistemaOperacional() {
        return this.SistemaOperacional_idSistemaOperacional;
    }

    public Programas Programas_idProgramas() {
        return this.Programas_idProgramas;
    }

    public Licenca Licenca_idLicenca() {
        return this.Licenca_idLicenca;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idPrograma(int idPrograma) {
        boolean ok = update("idPrograma", idPrograma);
        if (ok) {
            this.idPrograma = idPrograma;
        }
        return ok;
    }
    public boolean SistemaOperacional_idSistemaOperacional(SistemaOperacional SistemaOperacional_idSistemaOperacional) {
        boolean ok = update("SistemaOperacional_idSistemaOperacional", SistemaOperacional_idSistemaOperacional.idSistemaOperacional());
        if (ok) {
            this.SistemaOperacional_idSistemaOperacional = SistemaOperacional_idSistemaOperacional;
        }
        return ok;
    }
    public boolean Programas_idProgramas(Programas Programas_idProgramas) {
        boolean ok = update("Programas_idProgramas", Programas_idProgramas.idProgramas());
        if (ok) {
            this.Programas_idProgramas = Programas_idProgramas;
        }
        return ok;
    }
    public boolean Licenca_idLicenca(Licenca Licenca_idLicenca) {
        boolean ok = update("Licenca_idLicenca", Licenca_idLicenca.idLicenca());
        if (ok) {
            this.Licenca_idLicenca = Licenca_idLicenca;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Programa \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idPrograma = '" + idPrograma + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Programa> searchIdPrograma(int valor) {
        return search("idPrograma", valor+"");
    }
    public static java.util.ArrayList<Programa> searchSistemaOperacional_idSistemaOperacional(SistemaOperacional valor) {
        return search("SistemaOperacional_idSistemaOperacional", valor.idSistemaOperacional()+"");
    }
    public static java.util.ArrayList<Programa> searchProgramas_idProgramas(Programas valor) {
        return search("Programas_idProgramas", valor.idProgramas()+"");
    }
    public static java.util.ArrayList<Programa> searchLicenca_idLicenca(Licenca valor) {
        return search("Licenca_idLicenca", valor.idLicenca()+"");
    }

    public static java.util.ArrayList<Programa> search(String campo, String valor) {
        try {
            java.util.ArrayList<Programa> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idPrograma FROM Programa "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Programa(rs.getInt("idPrograma")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Programa newPrograma(int idPrograma, SistemaOperacional SistemaOperacional_idSistemaOperacional, Programas Programas_idProgramas, Licenca Licenca_idLicenca){
            String sql = "INSERT INTO Programa (idPrograma, SistemaOperacional_idSistemaOperacional, Programas_idProgramas, Licenca_idLicenca)"
                    + " VALUES "
                    + "('"+idPrograma+"', '"+SistemaOperacional_idSistemaOperacional.idSistemaOperacional() + "', '"+Programas_idProgramas.idProgramas() + "', '"+Licenca_idLicenca.idLicenca() + "');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Programa(rs.getInt("idPrograma"));
            } else {
                return new Programa(idPrograma);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}